Conditions | 6 |
Paths | 12288 |
Total Lines | 456 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | function smf_fileUpload(oOptions) |
||
2 | { |
||
3 | // Check if the file should be accepted or not... |
||
4 | Dropzone.prototype.accept = function(file, done) { |
||
|
|||
5 | if ((this.options.maxFiles != null) && this.getAcceptedFiles().length >= this.options.maxFiles) { |
||
6 | done(this.options.dictMaxFilesExceeded); |
||
7 | return this.emit("maxfilesexceeded", file); |
||
8 | } else |
||
9 | return this.options.accept.call(this, file, done); |
||
10 | }; |
||
11 | |||
12 | var previewNode = document.querySelector('#au-template'); |
||
13 | previewNode.id = ''; |
||
14 | var previewTemplate = previewNode.parentNode.innerHTML; |
||
15 | previewNode.parentNode.removeChild(previewNode); |
||
16 | |||
17 | // Default values in case oOptions isn't defined. |
||
18 | var dOptions = { |
||
19 | url: smf_prepareScriptUrl(smf_scripturl) + 'action=uploadAttach;sa=add;' + smf_session_var + '=' + smf_session_id + (current_board ? ';board=' + current_board : ''), |
||
20 | parallelUploads : 1, |
||
21 | filesizeBase:1024, |
||
22 | paramName: 'attachment', |
||
23 | uploadMultiple:true, |
||
24 | previewsContainer: '#au-previews', |
||
25 | previewTemplate: previewTemplate, |
||
26 | acceptedFiles: '.doc,.gif,.jpg,.pdf,.png,.txt,.zip', |
||
27 | thumbnailWidth: 100, |
||
28 | thumbnailHeight: null, |
||
29 | autoQueue: false, |
||
30 | clickable: '.fileinput-button', |
||
31 | smf_insertBBC: function(file, w, h){ |
||
32 | |||
33 | var mime_type = typeof file.type !== "undefined" ? file.type : (typeof file.mime_type !== "undefined" ? file.mime_type : ''); |
||
34 | |||
35 | var bbcOptionalParams = { |
||
36 | width: mime_type.indexOf('image') == 0 && +w > 0 ? (' width='+ w) : '', |
||
37 | height: mime_type.indexOf('image') == 0 && +h > 0 ? (' height='+ h) : '', |
||
38 | name: typeof file.name !== "undefined" ? (' name='+ file.name) : '', |
||
39 | type: ' type=' + mime_type, |
||
40 | }; |
||
41 | |||
42 | return '[attach' + bbcOptionalParams.width + bbcOptionalParams.height + decodeURIComponent(bbcOptionalParams.name) + bbcOptionalParams.type +']' + file.attachID + '[/attach]'; |
||
43 | }, |
||
44 | createMaxSizeBar: function(){ |
||
45 | |||
46 | // Update the MaxSize bar to reflect the new size percentage. |
||
47 | var range_maxFile = Math.round($.fn.percentToRange($.fn.rangeToPercent(myDropzone.options.totalMaxSize, 0, myDropzone.options.maxLimitReferenceUploadSize), 0, 100)); |
||
48 | |||
49 | // 3 basic colors. |
||
50 | if (range_maxFile <= 33) |
||
51 | range_maxFile_class = 'green'; |
||
52 | |||
53 | else if (range_maxFile >= 34 && range_maxFile <= 66) |
||
54 | range_maxFile_class = 'yellow'; |
||
55 | |||
56 | else |
||
57 | range_maxFile_class = 'red'; |
||
58 | |||
59 | $('#maxFiles_progress').show(); |
||
60 | $('#maxFiles_progress_text').show(); |
||
61 | $('#maxFiles_progress').removeClass().addClass('progressBar progress_'+ range_maxFile_class); |
||
62 | $('#maxFiles_progress span').width(range_maxFile + '%'); |
||
63 | |||
64 | // Show or update the text. |
||
65 | $('#maxFiles_progress_text').text(myDropzone.options.text_max_size_progress.replace('{currentTotal}', (Math.round(myDropzone.options.maxLimitReferenceUploadSize / 1024))).replace('{currentRemain}', Math.round(myDropzone.options.totalMaxSize / 1024))); |
||
66 | |||
67 | if (myDropzone.options.totalMaxSize == 0){ |
||
68 | $('#maxFiles_progress').hide(); |
||
69 | $('#maxFiles_progress_text').hide(); |
||
70 | } |
||
71 | }, |
||
72 | accept: function(file, done) { |
||
73 | |||
74 | // Need to check if the added file doesn't surpass the total max size setting. |
||
75 | myDropzone.options.totalMaxSize = myDropzone.options.totalMaxSize + file.size; |
||
76 | |||
77 | // This file has reached the max total size per post. |
||
78 | if (myDropzone.options.maxLimitReferenceUploadSize > 0 && myDropzone.options.totalMaxSize > myDropzone.options.maxLimitReferenceUploadSize){ |
||
79 | done(myDropzone.options.text_totalMaxSize.replace('{currentTotal}', Math.round(myDropzone.options.maxLimitReferenceUploadSize / 1024)).replace('{currentRemain}', Math.round(myDropzone.options.totalMaxSize / 1024))); |
||
80 | |||
81 | // File is cancel. |
||
82 | file.status = Dropzone.CANCELED; |
||
83 | } |
||
84 | |||
85 | // The file is too big. |
||
86 | if ((myDropzone.options.maxFilesize > 0) && (file.size > (myDropzone.options.maxFilesize * 1024))){ |
||
87 | done(myDropzone.options.dictFileTooBig); |
||
88 | |||
89 | // File is cancel. |
||
90 | file.status = Dropzone.CANCELED; |
||
91 | |||
92 | // File wasn't accepted so remove its size. |
||
93 | myDropzone.options.totalMaxSize = myDropzone.options.totalMaxSize - file.size; |
||
94 | } |
||
95 | else{ |
||
96 | |||
97 | myDropzone.options.createMaxSizeBar(); |
||
98 | |||
99 | // All done! |
||
100 | done(); |
||
101 | } |
||
102 | }, |
||
103 | totalMaxSize: 0 |
||
104 | }; |
||
105 | |||
106 | if(oOptions.thumbnailHeight && oOptions.thumbnailWidth) { |
||
107 | if(oOptions.thumbnailHeight > oOptions.thumbnailWidth) { |
||
108 | oOptions.thumbnailWidth = null; |
||
109 | } |
||
110 | |||
111 | else { |
||
112 | oOptions.thumbnailHeight = null; |
||
113 | } |
||
114 | } |
||
115 | |||
116 | $.extend(true, dOptions, oOptions); |
||
117 | |||
118 | |||
119 | var myDropzone = new Dropzone('div#attachUpload', dOptions); |
||
120 | |||
121 | myDropzone.on('addedfile', function(file) { |
||
122 | |||
123 | _thisElement = $(file.previewElement); |
||
124 | |||
125 | // If the attachment is an image and has a thumbnail, show it. Otherwise fallback to the generic thumbfile. |
||
126 | if (!file.type.match(/image.*/)) { |
||
127 | myDropzone.emit('thumbnail', file, smf_images_url +'/generic_attach.png'); |
||
128 | } |
||
129 | |||
130 | // If the file is too small, it won't have a thumbnail, show the regular file. |
||
131 | else if (typeof file.isMock !== "undefined" && typeof file.attachID !== "undefined") { |
||
132 | myDropzone.emit('thumbnail', file, smf_prepareScriptUrl(smf_scripturl) +'action=dlattach;attach='+ (file.thumbID > 0 ? file.thumbID : file.attachID) + ';type=preview'); |
||
133 | } |
||
134 | |||
135 | file.name = file.name.php_to8bit().php_urlencode(); |
||
136 | |||
137 | // Show the file info. |
||
138 | _thisElement.find('.attach-ui').fadeIn(); |
||
139 | |||
140 | // Create a function to insert the BBC attach tag. |
||
141 | file.insertAttachment = function (_innerElement, response){ |
||
142 | insertButton = $('<a />') |
||
143 | .addClass('button_submit') |
||
144 | .prop('disabled', false) |
||
145 | .text(myDropzone.options.text_insertBBC) |
||
146 | .on('click', function (e) { |
||
147 | e.preventDefault(); |
||
148 | |||
149 | w = _innerElement.find('input[name="attached_BBC_width"]').val(); |
||
150 | h = _innerElement.find('input[name="attached_BBC_height"]').val(); |
||
151 | |||
152 | // Get the editor stuff. |
||
153 | var oEditor = $('#' + oEditorID).data('sceditor'); |
||
154 | |||
155 | oEditor.insert(myDropzone.options.smf_insertBBC(response, w, h)); |
||
156 | }) |
||
157 | .appendTo(_innerElement.find('.attach-ui')); |
||
158 | }; |
||
159 | |||
160 | // Replace the filled with a message when the attachment is deleted. |
||
161 | file.deleteAttachment = function (_innerElement, attachmentId, file){ |
||
162 | |||
163 | deleteButton = $('<a />') |
||
164 | .addClass('button_submit') |
||
165 | .prop('disabled', false) |
||
166 | .text(myDropzone.options.text_deleteAttach) |
||
167 | .one('click', function (e) { |
||
168 | |||
169 | $this = $(this); |
||
170 | |||
171 | // Perform the action only after receiving the confirmation. |
||
172 | if (!confirm(smf_you_sure)){ |
||
173 | return; |
||
174 | } |
||
175 | |||
176 | // Let the server know you want to delete the file you just recently uploaded... |
||
177 | $.ajax({ |
||
178 | url: smf_prepareScriptUrl(smf_scripturl) + 'action=uploadAttach;sa=delete;attach='+ attachmentId +';' + smf_session_var + '=' + smf_session_id + (current_board ? ';board=' + current_board : ''), |
||
179 | type: 'GET', |
||
180 | dataType: 'json', |
||
181 | beforeSend: function(){ |
||
182 | ajax_indicator(true); |
||
183 | }, |
||
184 | complete: function(jqXHR, textStatus){ |
||
185 | ajax_indicator(false); |
||
186 | |||
187 | // Delete the button. |
||
188 | $this.fadeOutAndRemove('slow'); |
||
189 | }, |
||
190 | success: function (data, textStatus, xhr) { |
||
191 | |||
192 | // For dramatic purposes only! |
||
193 | _innerElement.removeClass('infobox').addClass(data.type +'box'); |
||
194 | |||
195 | // Remove the text field and show a nice confirmation message. |
||
196 | _innerElement.find('.attached_BBC').text(data.text); |
||
197 | _thisElement.find('.attach-info a.insertBBC').fadeOut(); |
||
198 | |||
199 | // Do stuff only if the file was actually accepted and it doesn't have an error status. |
||
200 | if (file.accepted && file.status != Dropzone.ERROR) { |
||
201 | |||
202 | // Need to remove the file size to make sure theres plenty of room for another one. |
||
203 | myDropzone.options.totalMaxSize = myDropzone.options.totalMaxSize - file.size; |
||
204 | |||
205 | // Re-count! |
||
206 | myDropzone.options.createMaxSizeBar(); |
||
207 | } |
||
208 | }, |
||
209 | error: function (xhr, textStatus, errorThrown) { |
||
210 | |||
211 | // Tell the user something horrible happen! |
||
212 | _innerElement.find('span.error').append(textStatus.error.join('<br>')); |
||
213 | |||
214 | // For dramatic purposes only! |
||
215 | _innerElement.removeClass('infobox').addClass('errorbox'); |
||
216 | } |
||
217 | }); |
||
218 | }) |
||
219 | .appendTo(_innerElement.find('.attach-ui')); |
||
220 | }; |
||
221 | |||
222 | // Hookup the upload button. |
||
223 | _thisElement.find('.upload').on( 'click', function() { |
||
224 | myDropzone.enqueueFile(file); |
||
225 | }); |
||
226 | |||
227 | // Show the main stuff! |
||
228 | _thisElement.addClass('descbox'); |
||
229 | |||
230 | // Show the upload and cancel all buttons only if there is something to cancel/upload. |
||
231 | if (myDropzone.getFilesWithStatus(Dropzone.ADDED).length == 1){ |
||
232 | $('div#attachUpload').find('#attach-cancelAll, #attach-uploadAll').fadeIn(); |
||
233 | } |
||
234 | }); |
||
235 | |||
236 | // Stuff to do when a file gets cancel. |
||
237 | myDropzone.on('removedfile', function(file) { |
||
238 | |||
239 | // Do stuff only if the file was actually accepted and it doesn't have an error status. |
||
240 | if (file.accepted && file.status != Dropzone.ERROR) { |
||
241 | |||
242 | // Need to remove the file size to make sure theres plenty of room for another one. |
||
243 | myDropzone.options.totalMaxSize = myDropzone.options.totalMaxSize - file.size; |
||
244 | |||
245 | // Re-count! |
||
246 | myDropzone.options.createMaxSizeBar(); |
||
247 | } |
||
248 | |||
249 | // Hide the cancel and upload all buttons if there is nothing to cancel/upload anymore. |
||
250 | if (myDropzone.getFilesWithStatus(Dropzone.ADDED).length == 0){ |
||
251 | $('div#attachUpload').find('#attach-cancelAll, #attach-uploadAll').fadeOut(); |
||
252 | } |
||
253 | }); |
||
254 | |||
255 | // Update the total progress bar. |
||
256 | myDropzone.on('totaluploadprogress', function(progress) { |
||
257 | $('#total-progress span').width(progress + '%'); |
||
258 | }); |
||
259 | |||
260 | myDropzone.on('error', function(file, errorMessage, xhr) { |
||
261 | |||
262 | _thisElement = $(file.previewElement); |
||
263 | |||
264 | // Remove the 'upload' button. |
||
265 | _thisElement.find('.upload').fadeOutAndRemove('slow'); |
||
266 | |||
267 | // Set a nice css class to make it more obvious theres an error. |
||
268 | _thisElement.addClass('errorbox').removeClass('descbox'); |
||
269 | }); |
||
270 | |||
271 | myDropzone.on('success', function(file, responseText, e) { |
||
272 | |||
273 | _thisElement = $(file.previewElement); |
||
274 | |||
275 | // Remove the 'upload' button. |
||
276 | _thisElement.find('.upload').fadeOutAndRemove('slow'); |
||
277 | |||
278 | // Don't do anything if there is no response from server. |
||
279 | if (!responseText){ |
||
280 | return; |
||
281 | } |
||
282 | |||
283 | // There is a general error. |
||
284 | if (responseText.generalErrors){ |
||
285 | _thisElement.find('span.error').append(responseText.generalErrors.join('<br>')); |
||
286 | return; |
||
287 | } |
||
288 | |||
289 | // Server returns an array. |
||
290 | response = responseText.files[0]; |
||
291 | |||
292 | // Show the input field and insert button. |
||
293 | _thisElement.find('.attach-info div.attached_BBC').fadeIn(); |
||
294 | _thisElement.find('.attach-info a.insertBBC').fadeIn(); |
||
295 | |||
296 | if (typeof response.mime_type == "undefined" || response.mime_type.indexOf('image') != 0){ |
||
297 | _thisElement.find('.attach-info .attached_BBC_width_height').hide(); |
||
298 | } |
||
299 | |||
300 | // The request was complete but the server returned an error. |
||
301 | if (typeof response.errors !== 'undefined' && response.errors.length > 0){ |
||
302 | |||
303 | _thisElement.addClass('errorbox').removeClass('descbox'); |
||
304 | |||
305 | // Show the server error. |
||
306 | _thisElement.find('span.error').append(response.errors.join('<br>')); |
||
307 | return; |
||
308 | } |
||
309 | |||
310 | // If there wasn't any error, change the current cover. |
||
311 | _thisElement.addClass('infobox').removeClass('descbox'); |
||
312 | |||
313 | // Append the BBC. |
||
314 | w = _thisElement.find('input[name="attached_BBC_width"]').val(); |
||
315 | h = _thisElement.find('input[name="attached_BBC_height"]').val(); |
||
316 | _thisElement.find('input[name="attachBBC"]').val(myDropzone.options.smf_insertBBC(response, w, h)); |
||
317 | |||
318 | file.insertAttachment(_thisElement, response); |
||
319 | |||
320 | // You have already loaded this attachment, to prevent abuse, you cannot cancel it and upload a new one. |
||
321 | _thisElement.find('a.cancel').fadeOutAndRemove('slow'); |
||
322 | |||
323 | // Fire up the delete button. |
||
324 | file.deleteAttachment(_thisElement, response.attachID, file); |
||
325 | }); |
||
326 | |||
327 | myDropzone.on('uploadprogress', function(file, progress, bytesSent) { |
||
328 | |||
329 | _thisElement = $(file.previewElement); |
||
330 | |||
331 | // Get the current file box progress bar, set its inner span's width accordingly. |
||
332 | _thisElement.find('div.progressBar span').width(progress + '%'); |
||
333 | }); |
||
334 | |||
335 | myDropzone.on('complete', function(file, progress, bytesSent) { |
||
336 | |||
337 | _thisElement = $(file.previewElement); |
||
338 | |||
339 | // Hide the progress bar. |
||
340 | _thisElement.find('div.progressBar').fadeOut(); |
||
341 | |||
342 | // Finishing up mocking! |
||
343 | if (typeof file.isMock !== "undefined" && typeof file.attachID !== "undefined"){ |
||
344 | // Show the input field. |
||
345 | _thisElement.find('.attach-info div.attached_BBC').fadeIn(); |
||
346 | _thisElement.find('.attach-info a.insertBBC').fadeIn(); |
||
347 | |||
348 | if (typeof file.type == "undefined" || file.type.indexOf('image') != 0){ |
||
349 | _thisElement.find('.attach-info .attached_BBC_width_height').hide(); |
||
350 | } |
||
351 | |||
352 | // If there wasn't any error, change the current cover. |
||
353 | _thisElement.addClass('infobox').removeClass('descbox'); |
||
354 | |||
355 | // Remove the 'upload' button. |
||
356 | _thisElement.find('.upload').fadeOutAndRemove('slow'); |
||
357 | |||
358 | // Append the BBC. |
||
359 | w = _thisElement.find('input[name="attached_BBC_width"]').val(); |
||
360 | h = _thisElement.find('input[name="attached_BBC_height"]').val(); |
||
361 | _thisElement.find('input[name="attachBBC"]').val(myDropzone.options.smf_insertBBC(file, w, h)); |
||
362 | |||
363 | file.insertAttachment(_thisElement, file); |
||
364 | |||
365 | // You have already loaded this attachment, to prevent abuse, you cannot cancel it and upload a new one. |
||
366 | _thisElement.find('a.cancel').fadeOutAndRemove('slow'); |
||
367 | |||
368 | // Fire up the delete button. |
||
369 | file.deleteAttachment(_thisElement, file.attachID, file); |
||
370 | |||
371 | // Need to count this towards the max limit. |
||
372 | myDropzone.options.totalMaxSize = myDropzone.options.totalMaxSize + file.size; |
||
373 | |||
374 | // Re-count and display the bar. |
||
375 | myDropzone.options.createMaxSizeBar(); |
||
376 | } |
||
377 | }); |
||
378 | |||
379 | // Show each individual's progress bar. |
||
380 | myDropzone.on('sending', function(file, xhr, formData) { |
||
381 | |||
382 | _thisElement = $(file.previewElement); |
||
383 | |||
384 | // Show the progress bar when upload starts. |
||
385 | _thisElement.find('div.progressBar').fadeIn(); |
||
386 | |||
387 | // Show the total progress bar when upload starts. |
||
388 | $("#total-progress").fadeIn(); |
||
389 | }); |
||
390 | |||
391 | // Update the total progress bar. |
||
392 | myDropzone.on("totaluploadprogress", function(progress) { |
||
393 | $("#total-progress span").width(progress + '%'); |
||
394 | }); |
||
395 | |||
396 | // Hide the total progress bar when nothing's uploading anymore. |
||
397 | myDropzone.on("queuecomplete", function(progress) { |
||
398 | $("#total-progress").fadeOut(); |
||
399 | }); |
||
400 | |||
401 | // Add an event for uploading and cancelling all files. |
||
402 | $('a#attach-cancelAll' ).on('click', function() { |
||
403 | |||
404 | if (!confirm(smf_you_sure)){ |
||
405 | return; |
||
406 | } |
||
407 | |||
408 | myDropzone.removeAllFiles(true); |
||
409 | myDropzone.options.createMaxSizeBar(); |
||
410 | }); |
||
411 | |||
412 | $('a#attach-uploadAll' ).on('click', function() { |
||
413 | |||
414 | if (!confirm(smf_you_sure)){ |
||
415 | return; |
||
416 | } |
||
417 | |||
418 | myDropzone.enqueueFiles(myDropzone.getFilesWithStatus(Dropzone.ADDED)); |
||
419 | myDropzone.options.createMaxSizeBar(); |
||
420 | }); |
||
421 | |||
422 | // Need to tell the user they cannot post until all files are either uploaded or canceled. |
||
423 | $("input[name ='post']").on('click', function(e) { |
||
424 | |||
425 | attachAdded = myDropzone.getFilesWithStatus(Dropzone.ADDED).length; |
||
426 | attachQueued = myDropzone.getFilesWithStatus(Dropzone.QUEUED).length; |
||
427 | |||
428 | if (attachAdded > 0 || attachQueued > 0 ){ |
||
429 | alert(myDropzone.options.text_attachLeft); |
||
430 | e.preventDefault(); |
||
431 | e.stopPropagation(); |
||
432 | return false; |
||
433 | } |
||
434 | }); |
||
435 | |||
436 | // Hide the default way to show already atached files. |
||
437 | $('#postAttachment').fadeOutAndRemove('slow'); |
||
438 | |||
439 | // Show any attachments already uploaded. |
||
440 | if (typeof current_attachments !== "undefined"){ |
||
441 | $.each(current_attachments, function(key, mock) { |
||
442 | |||
443 | // Tell the world this is a mock file! |
||
444 | mock.isMock = true; |
||
445 | |||
446 | // Tell eveyone this file was accepted. |
||
447 | mock.status = Dropzone.ADDED; |
||
448 | mock.accepted = true; |
||
449 | |||
450 | myDropzone.emit("addedfile", mock); |
||
451 | |||
452 | // This file is "completed". |
||
453 | myDropzone.emit("complete", mock); |
||
454 | }); |
||
455 | } |
||
456 | } |
||
457 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.